package com.mutouren.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;

import com.mutouren.common.exception.ExceptionUtils;

public class PropertiesUtils {
	
	public static Map<String, String> getConfig(String path) {		
		return getConfig(path, "UTF-8");
	}
	
	public static Map<String, String> getConfig(String path, String charsetName) {
		Map<String, String> mapResult = new HashMap<String, String>();
		
		InputStream inStream = PropertiesUtils.class.getResourceAsStream(path);
		Properties properties = new Properties();
		try {
			InputStreamReader inReader = new InputStreamReader(inStream, charsetName);
			properties.load(inReader);
			
			Set<Entry<Object, Object>> setProperties = properties.entrySet();
			Iterator<Map.Entry<Object, Object>> iterator = setProperties.iterator();
			
			while (iterator.hasNext()) {
				String key = null, value = null;
				Entry<Object, Object> entry = iterator.next();
				
				key = String.valueOf(entry.getKey());
				value = String.valueOf(entry.getValue());
				key = key == null ? key : key.trim();
				value = value == null ? value : value.trim();
				
				if (key == null) continue;				
				mapResult.put(key, value);
			}			
		} catch (IOException e) {
			throw ExceptionUtils.doUnChecked(e);
		}
		
		return mapResult;
	}

	public static void main(String[] args) {
/*		String path = "/mtr.properties";
		Map<String, String> map = PropertiesUtils.getConfig(path); 
		System.out.println(map);*/
	}
	
}

